<template>
  <view> hello world </view>
</template>

<script>
const DIR_NAME = "test";
const fileName = "pay.html";
const filePath = "_doc/test/" + fileName;

export default {
  data() {
    return {
      html: `hello world`
    };
  },
  async onLoad(options) {
    await this.fileWriter(this.html + new Date().toUTCString());

    /**
     * 获取文件的内容
     * fileName:本地的文件地址
     * result：文件的数据
     */
    const content = await this.getFileContext(filePath);
    console.log("****************** start ********************");
    console.log(content);
  },
  methods: {
    fileWriter(str) {
      return new Promise(async (resolve) => {
        let dirEntry = await this.getDirEntry(DIR_NAME); //创建、获取文件夹
        let fileEntry = await this.getFileEntry(fileName, dirEntry); // 创建、获取文件

        // 写入文件
        fileEntry.createWriter(function (writer) {
          writer.seek(0);
          writer.write(str);

          resolve();
        });
      });
    },

    async getFileEntry(fileName, dirEntry) {
      return new Promise((resolve) => {
        plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
          let entry = dirEntry || fs.root;
          entry.getFile(
            fileName,
            {
              create: true
            },
            function (fileEntry) {
              resolve(fileEntry);
            }
          );
        });
      });
    },

    getDirEntry(dirName) {
      return new Promise(async (resolve) => {
        plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
          fs.root.getDirectory(
            dirName,
            {
              create: true
            },
            function (dirEntry) {
              resolve(dirEntry);
            }
          );
        });
      });
    },

    async getFile(fileName, dirEntry) {
      return new Promise(async (resolve) => {
        let fileEntry = await this.getFileEntry(fileName, dirEntry);
        fileEntry.file(function (file) {
          resolve(file);
        });
      });
    },

    async getFileContext(path, dirEntry) {
      let deffered;
      let fileReader = new plus.io.FileReader();
      fileReader.onloadend = function (evt) {
        deffered(evt.target);
      };
      let file = await this.getFile(path, dirEntry);
      fileReader.readAsText(file, "utf-8");
      return new Promise((resolve) => {
        deffered = resolve;
      });
    },

    removeFile(dirEntry, fileName) {
      plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
        let entry = dirEntry || fs.root;
        let directoryReader = entry.createReader();
        directoryReader.readEntries(function (entries) {
          for (let i = entries.length - 1; i >= 0; i--) {
            if (fileName) {
              if (fileName === entries[i].name) {
                console.log("删除文件", entries[i].name);
                entries[i].remove();
              }
            } else {
              entries[i].remove();
            }
          }
        });
      });
    },

    async existFile(fileName, dirEntry) {
      return new Promise((resolve) => {
        plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
          let entry = dirEntry || fs.root;
          let directoryReader = entry.createReader();
          directoryReader.readEntries(function (entries) {
            let isExist = entries.some((entry) => entry.name === fileName);
            resolve(isExist);
          });
        });
      });
    },

    async iterateDierctory(dirEntry) {
      return new Promise((resolve) => {
        plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
          let entry = dirEntry || fs.root;
          let directoryReader = entry.createReader();
          directoryReader.readEntries(
            function (entries) {
              entries.forEach((item, idx, arr) => {
                if (idx === 0)
                  console.log(
                    "---------------" + entry.name + "目录-----------------"
                  );
                console.log(idx + 1, item.name);
                if (idx === arr.length - 1)
                  console.log("---------------end-----------------");
              });
              resolve(entries);
            },
            function (e) {
              console.log("Read entries failed: " + e.message);
            }
          );
        });
      });
    }
  }
};
</script>
